/**
 *
 * 2007-11-2 上午10:47:10
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartNode;

public class StartNodeLoader extends NodeLoader {

	protected SaturnStartNode createStartNode() {
		return new SaturnStartNode();
	}

	public List<Shape> loadNodes(Element root) {
		NodeList startNodes = root
				.getElementsByTagName(SaturnStartNode.KEY_NODE_START);

		List<Shape> shapes = new ArrayList<Shape>(startNodes.getLength());

		for (int i = 0; i < startNodes.getLength(); ++i) {
			SaturnStartNode saturnStartNode = createStartNode();
			Node startNode = startNodes.item(i);
			saturnStartNode.setNode(startNode);
			String shapeId = super.getNodeAttributeValue(startNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(startNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			String name = super.getNodeElementValue(startNode,
					SaturnStartNode.SHOW_START_NODE_NAME);

			String autorollback = this.getGlobalTransactionValue(root,
					saturnStartNode);

			String description = super.getNodeElementValue(startNode,
					SaturnStartNode.SHOW_START_NODE_DESCRITPION);
			List<SaturnParameter> parameters = super.getParameters(startNode);

			Point location = super.getLocation(startNode);
			Dimension size = new Dimension(50, 70);

			
			saturnStartNode.setId(shapeId);
			saturnStartNode.setLocation(location);
			saturnStartNode.setName(name);
			saturnStartNode.setAutorollback(autorollback);
			saturnStartNode.setNextId(nextId);
			saturnStartNode.setDescription(description);
			saturnStartNode.setParameters(parameters);
			saturnStartNode.setSize(size);

			shapes.add(saturnStartNode) ;
		}

		return shapes;
	}

	/**
	 * 载入Start的globalTransaction
	 * 
	 */
	private String getGlobalTransactionValue(Element element,
			SaturnStartNode saturnStartNode) {

		Node node = saturnStartNode.getNode();

		Node tTypeNode = super.getChildNode(node,
				SaturnStartNode.SHOW_START_NODE_TRANSACTION);

		String tTypeTemp = "false";
		try {
			if (tTypeNode == null) {
				return tTypeTemp;
			}
			
			tTypeTemp = super.getNodeAttributeValue(tTypeNode,
					SaturnStartNode.SHOW_START_NODE_TRANSACTIONAUTOROLLBACK);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return tTypeTemp;
	}
}
